package ui {
import com.greensock.TweenLite;
import com.greensock.TweenLite;
import core.Facade;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;

import ru.cjay.loading.UnsafeConnection;

//import flash.net.URLRequest;

import ru.cjay.loading.JSONLoader;

public class ZoneInfoPopup {

    private static var _instance:ZoneInfoPopup;
    private static var allowInstantiation:Boolean;

    private var _window:InfoWindowMC;
    private var _showed:Boolean = false;

    public static function getInstance():ZoneInfoPopup {
        if (_instance == null) {
            allowInstantiation = true;
            _instance = new ZoneInfoPopup();
            allowInstantiation = false;
        }
        return _instance;
    }

    public function ZoneInfoPopup() {
        if (!allowInstantiation) {
            throw new Error("Error: Instantiation failed: Use ZoneInfoPopup.getInstance() instead of new.");
        }
    }

    public function show(_zone_id:int):void {
        if (_window == null) {
            _window = new InfoWindowMC();

        }
        if (!_showed) {
            Facade.getInstance().fadeIn();
            _window.window.alpha = 0;
            Facade.getInstance().stage.addChild(_window.window);
            _window.window.x = (Facade.WIDTH - _window.window.width) / 2;
            _window.window.y = (Facade.HEIGHT - _window.window.height) / 2;

            // Запрос состояния зоны
            var loader:JSONLoader = new JSONLoader();
            loader.loadJSON("http://vd.osa-corp.ru/map/gamezone?zone_id=" + _zone_id +"&"+ Facade.getInstance().authUrlParams(), updateInfo);
        }


        _window.addEventListener("event_close_me", function(event:Event) {
            TweenLite.to(_window.window, 0.3, {alpha:0, onComplete:function() {
                        _showed = false;
                        Facade.getInstance().fadeOut();
                        if (Facade.getInstance().stage.contains(_window.window))
                            Facade.getInstance().stage.removeChild(_window.window);
                    }
                    })
        });
    }

    private function updateInfo(obj:Object):void {
        if (obj["type"] != "ok") {
            var warningWindow:IconMessageBox = new IconMessageBox();
            var stage:Stage = Facade.getInstance().stage;

            stage.addChild(warningWindow);
            warningWindow.caption.text = "ОШИБКА";
            warningWindow.message.text = obj.message;
            warningWindow.x = (640 - warningWindow.width) * 0.5;
            warningWindow.y = (640 - warningWindow.height) * 0.5;
            warningWindow.alpha = 1;
            warningWindow.scaleZ = 0.5;
            stage.setChildIndex(warningWindow, stage.numChildren - 1);
            return;
        }
        _window.window.caption.text = obj.data.name;
        _window.window.info.text = obj.data.description;
        _window.window.subjects_lst.icon_text.text= obj.data.require_campaign.name;
        var icon:Sprite = UnsafeConnection.UnsafeLoader(obj.data.require_campaign.img);
        _window.window.subjects_lst.icon.addChild(icon);
        _window.fill_dependencies_list(obj.data);
        TweenLite.to(_window.window, 0.3,{alpha:1});


    }
}
}
